package com.zzh.partnersys;

import cn.hutool.json.JSONObject;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

public class AmapLocationWithApacheHttpClient2 {
    public static void main(String[] args) {
        // 替换为你的高德地图API Key
        String apiKey = "66f0747300645759892c9af3a339b955";
        String ip = "114.247.50.2";
        String url = "https://restapi.amap.com/v3/geocode/regeo?key=%s&location=%s,%s";
        double lng = 113.1017375;
        double lat = 22.93212254;
        String urlAddress = String.format(url, apiKey, lng, lat);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(urlAddress);
        try {
            CloseableHttpResponse response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("响应状态码: " + statusCode);
            String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
            JSONObject jsonObject = new JSONObject(responseBody);
            System.out.println(responseBody);
            if (statusCode == 200) {
                JSONObject regeocode = jsonObject.getJSONObject("regeocode");
                String formattedAddress = regeocode.getStr("formatted_address");
                System.out.println("地址: " + formattedAddress);
            }
        } catch (IOException e) {
            System.out.println("发生错误: " + e.getMessage());
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                System.out.println("关闭HttpClient出错: " + e.getMessage());
            }
        }
    }
}